﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.Filter.Special
{

    [Serializable]
    public class ImgSrcRegexHTMLAnalyze : RegexHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "图片地址过滤器";
            }
        }


        public override string Description
        {
            get
            {
                return "获取img对象的图片地址";
            }
        }

        public ImgSrcRegexHTMLAnalyze()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "(?<=<\\s*img\\s+.*src\\s*=(\"|')?)https?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?(?=(\"|')?)";
        }
    }


    [Serializable]
    public class NextPageRegexHTMLAnalyze : RegexHTMLAnalyze
    {


        public override string FilterCaption
        {
            get
            {
                return "下页地址过滤器";
            }
        }

        public override string ExpressionCaption
        {
            get
            {
                return "下一页标识";
            }
        }

        public override string Description
        {
            get
            {
                return "获取下一页Url";
            }
        }


        public NextPageRegexHTMLAnalyze()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            //OutputType = 0;
            //ParseType = 0;
            FilterExpression = "下一页";
        }

        public override List<FilterResult> FilterHTML()
        {

            //string pattern = string.Format("<{0}*a ({0}*{1}*{0}*={0}*[\"']?{0}*([{1}:/._:;-]|[\u4E00-\u9FA5]|[\uFE30-\uFFA0])*{0}*[\"']?{0}*)*" +
            //                "({0}*href*{0}*={0}*[\"']?{0}*([{1}:/._:;-]|[\u4E00-\u9FA5]|[\uFE30-\uFFA0])*{0}*[\"']?{0}*)*" +
            //                "({0}*{1}*{0}*={0}*[\"']?{0}*([{1}:/._:;-]|[\u4E00-\u9FA5]|[\uFE30-\uFFA0])*{0}*[\"']?{0}*)*>{0}*{2}{0}*</{0}*a{0}*>", @"\s", @"\w", FilterExpression);

            string pattern = string.Format("<{0}*a({0}*{1}*{0}*={0}*[\"']?{0}*([{1}{0}:/._:;-]|[\u4E00-\u9FA5]|[\uFE30-\uFFA0])*{0}*[\"']?{0}*)+{0}*>{0}*下一页{0}*<{0}*/{0}*a{0}*>", @"\s", @"\w", FilterExpression);
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern);
            System.Text.RegularExpressions.MatchCollection matchs ;
            
            foreach (var pr in parentResults)
            {
                matchs = reg.Matches(pr.HTMLResult);
                foreach (System.Text.RegularExpressions.Match match in matchs)
                {
                    FilterResult result = new FilterResult(pr.HTMLPosition + match.Index, match.Value);
                    listRevalue.Add(result);
               }

            }
            return listRevalue; // 


            //System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern);
            //System.Text.RegularExpressions.MatchCollection matchs = reg.Matches(GetInputHTML());
            //foreach (System.Text.RegularExpressions.Match match in matchs)
            //{
            //    sb.AppendFormat("{0}", match.Value);
            //}

        }


    }



}
